package org.redr.widgetGUI.core;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QSizePolicy;
import com.trolltech.qt.gui.QTextCursor;
import com.trolltech.qt.gui.QTextEdit;

public class TextEdit extends WidgetState{
	
	QTextEdit textEdit;
	
	public QTextEdit widget(){
		return textEdit;
		
	}
	
	public TextEdit(LayoutState p, WidgetSettings ws){
		super(p, ws);
		GroupBox gb	=	new GroupBox(p, ws);
		textEdit = new QTextEdit();
		// textEdit.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
		gb.addWidget(textEdit);
		_loadSettingText();
		
	}
	
	public TextEdit(QLayout layout, WidgetSettings notesSettings) {
		super(layout, notesSettings);
		textEdit	=	new QTextEdit();
		// textEdit.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
		layout.addWidget(textEdit);
		_loadSettingText();
	}

	public void setHtml(String html){
		textEdit.setHtml(html);
		_setSettingText();
	}
	
	public void insertHtml(String html){
		textEdit.insertHtml(html);
		_setSettingText();
	}
	
	public void setText(String text){
		textEdit.setText(text);
		_setSettingText();
	}
	
	public void setPlainText(String text){
		textEdit.setPlainText(text);
		_setSettingText();
	}
	
	public String toHtml(){
		_setSettingText();
		return textEdit.toHtml();
	}
	
	public String toPlainText(){
		_setSettingText();
		return textEdit.toPlainText();
	}
	
	private void _setSettingText(){
		_setSetting("text", textEdit.toHtml());
	}
	
	private void _loadSettingText(){
		textEdit.clear();
		textEdit.insertHtml(_getSetting("text", ""));
	}

	public void setCursorToEnd(){
		QTextCursor cursor	=	textEdit.textCursor();
		cursor.movePosition(QTextCursor.MoveOperation.End);
		textEdit.setTextCursor(cursor);
		
	}
	public void append(String string) {
		setCursorToEnd();
		textEdit.append(string);
	}
	
	

}
